<?php
    class Feedback_con extends CI_Controller{
        function __construct() {
            parent::__construct();
            $this->load->library('session');
        }
        function index(){
            $this->login();
        }
        function login(){
            $this->load->view('feedback/feedback_header_view');
            $this->load->view('feedback/form_login');
            $this->load->view('feedback/feedback_footer_view');
        }
        function proses_login(){
            //mencegah ada yang langsung ke url proses_login, jenis error 0
            if(!isset($_POST['username'])||!isset($_POST['password'])){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            
            $usr = $this->input->post('username');
            $pwd = md5($this->input->post('password'));
            
            $query_peserta = $this->db->query('select * from user natural join role where USERNAME="'.$usr.'" and PASSWORD="'.$pwd.'" and ID_ROLE=9');
            if(count($query_peserta->result_array())!=1){
                //error jika ada kesalahan login, jenis error 1
                redirect(base_url().'feedback/feedback_con/error_display/1');
            }else{
                $tgl_now=date('Y-m-d', time());
                //melakukan query terhadap data peserta
                $query_peserta=$this->db->get_where('user',array('USERNAME'=>$usr,'PASSWORD'=>$pwd));
                $data_peserta=$query_peserta->row_array();
                
                $jam_sekarang = date('H.i',time());
                
                $query_sesi = $this->db->query('select * from peserta_kelas natural join daftar_jadwal where ID_USER='.$data_peserta['ID'].' and TANGGAL="'.$tgl_now.'"');
                $hasil = $query_sesi->result_array();               
                
                //error jika tidak ada sesi yang sedang aktif, jenis error 2
                if(count($hasil)==0){
                    redirect(base_url().'feedback/feedback_con/error_display/2');
                }
                
                $now = date_create($hasil[0]['JAM_SELESAI']);
                date_sub($now,date_interval_create_from_date_string('45 minutes'));
                $now = date_format($now, 'H.i');
                
                if($jam_sekarang<$now||$jam_sekarang>($hasil[0]['JAM_SELESAI']+0.15)){
                    redirect(base_url().'feedback/feedback_con/error_display/2');
                }
                
                $data_absen = array(
                    'ID'=>$data_peserta['ID'],
                    'KODE_PELATIHAN'=>$hasil[0]['KODE_PELATIHAN'],
                    'ANGKATAN'=>$hasil[0]['ANGKATAN'],
                    'NOMOR_SESI'=>$hasil[0]['NOMOR_SESI']
                );
                $cek_absen = $this->db->get_where('absen',$data_absen)->result_array();
                if(count($cek_absen)>0){
                    //error jika sudah mengisi SF untk sesi ini, jenis error 3
                    redirect(base_url().'feedback/feedback_con/error_display/3');
                }else{
                    //pengecekan apakah udah ngisi tapi blon keabsen
                    //data isian sebelumnya dihapus
                    $data_komentar_sesi = array(
                        'ID'=>$data_peserta['ID'],
                        'KODE_PELATIHAN'=>$hasil[0]['KODE_PELATIHAN'],
                        'ANGKATAN'=>$hasil[0]['ANGKATAN'],
                        'NOMOR_SESI'=>$hasil[0]['NOMOR_SESI']
                    );
                    $res = $this->db->get_where('komentar_sesi',$data_komentar_sesi)->result_array();
                    if(count($res)>0){
                        $this->db->delete('komentar_sesi', $data_komentar_sesi);
                    }
                    $data_nilai_pengajar = array(
                        'ID_PENILAI'=>$data_peserta['ID'],
                        'KODE_PELATIHAN'=>$hasil[0]['KODE_PELATIHAN'],
                        'ANGKATAN'=>$hasil[0]['ANGKATAN'],
                        'NOMOR_SESI'=>$hasil[0]['NOMOR_SESI']
                    );
                    $res = $this->db->get_where('nilai_pengajar',$data_nilai_pengajar)->result_array();
                    if(count($res)>0){
                        $this->db->delete('nilai_pengajar', $data_nilai_pengajar);
                    }
                    $data_jawaban = array(
                        'ID_USER'=>$data_peserta['ID'],
                        'KODE_PELATIHAN'=>$hasil[0]['KODE_PELATIHAN'],
                        'ANGKATAN'=>$hasil[0]['ANGKATAN'],
                        'NOMOR_SESI'=>$hasil[0]['NOMOR_SESI']
                    );
                    $res = $this->db->get_where('jawaban',$data_jawaban)->result_array();
                    if(count($res)>0){
                        $this->db->delete('jawaban', $data_jawaban);
                    }
                }               
                
                $query_data_peserta = $this->db->query('select * from peserta where ID='.$data_peserta['ID']);
                $peserta=$query_data_peserta->row_array();

                $query_data_pel = $this->db->query('select * from pelatihan where KODE_PELATIHAN="'.$hasil[0]['KODE_PELATIHAN'].'"');
                
                $data_pel = $query_data_pel->row_array();
                if($data_peserta['FOTO']==''){
                    $data_peserta['FOTO']=base_url().'foto/cencored.jpg';
                }
                $data_sesi=array(
                    'KODE_PELATIHAN'=>$hasil[0]['KODE_PELATIHAN'],
                    'ANGKATAN'=>$hasil[0]['ANGKATAN'],
                    'NOMOR_SESI'=>$hasil[0]['NOMOR_SESI']
                );
                $query_id_tema = $this->db->get_where('sesi_pelatihan',$data_sesi);
                $id_tema = $query_id_tema->row()->ID_TEMA;
                
                $jum_pertanyaan = $this->db->query('select distinct count(ID_PERTANYAAN) as jum from daftar_pertanyaan where ID_TEMA='.$id_tema)->row()->jum;
                
                $data_session = array(
                    'id'=>$data_peserta['ID'],
                    'nama'=>$data_peserta['NAMA'],
                    'foto'=>$data_peserta['FOTO'],
                    'kode'=>$hasil[0]['KODE_PELATIHAN'],
                    'angkatan'=>$hasil[0]['ANGKATAN'],
                    'no_sesi'=>$hasil[0]['NOMOR_SESI'],
                    'no_cic'=>$peserta['NO_CIC_CARD'],
                    'nama_pel'=>$data_pel['NAMA_PELATIHAN'],
                    'login'=>true,
                    'id_tema'=>$id_tema,
                    'jum_pertanyaan'=>$jum_pertanyaan
                );
                $this->session->set_userdata($data_session);
                
                redirect(base_url().'feedback/feedback_con/start_sf');
            }
        }
        function start_sf(){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            $id = $this->session->userdata('id');
            $angkatan=$this->session->userdata('angkatan');
            $kode=$this->session->userdata('kode');
            $cur_sesi=$this->session->userdata('no_sesi');
            
            $query_jum_sesi = $this->db->get_where('kelas',array('KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan));
            $jum_sesi = $query_jum_sesi->row_array();
            $total = $jum_sesi['JAM_TOTAL'];
            $persesi = $jum_sesi['JAM_PER_SESI'];
            
            $display['jum_sesi']=$total/$persesi;
            $jum_hadir=0;
            for($i=1;$i<=$display['jum_sesi'];$i++){
                $strquery = 'select * from absen where ID='.$id.' and KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan.' and NOMOR_SESI='.$i;
                $query_absensi = $this->db->query($strquery);
                if(count($query_absensi->result_array())==1){
                    $display['kehadiran'][$i]='O';
                    $jum_hadir++;
                }else
                    if($i<$cur_sesi)
                        $display['kehadiran'][$i]='X';
                    else
                        $display['kehadiran'][$i]='-';
            }
            
            $display['persentase']=$jum_hadir/$cur_sesi*100;
            
            $this->load->view('feedback/feedback_header_view');
            $this->load->view('feedback/welcome_sf',$display);
            $this->load->view('feedback/feedback_footer_view');
        }
        function step_one($message=''){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            if($message=='error'){
                $message='Anda harus mengisikan nilai pemahaman Anda sebelum melanjutkan';
            }
            $count['num']=1;
            $display['message']=$message;
            $this->load->view('feedback/feedback_header_view');
            $this->load->view('feedback/counter',$count);
            $this->load->view('feedback/step_one',$display);
            $this->load->view('feedback/feedback_footer_view');
        }
        function step_one_process(){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            if(!isset($_POST['score_peserta'])){
                redirect(redirect(base_url().'feedback/feedback_con/step_one/error'));
            }
            $skor = $this->input->post('score_peserta');
            
            
            if(isset($_POST['like_button'])){
                $like=true;
            }else{
                $like=false;
            }
            $data_komentar_sesi = array(
                'ID'=>$this->session->userdata('id'),
                'KODE_PELATIHAN'=>$this->session->userdata('kode'),
                'ANGKATAN'=>$this->session->userdata('angkatan'),
                'NOMOR_SESI'=>$this->session->userdata('no_sesi'),
                'LIKE'=>$like,
                'NILAI_PEMAHAMAN'=>$skor
            );
            $this->db->insert('komentar_sesi',$data_komentar_sesi);
            redirect(base_url().'feedback/feedback_con/step_two');
        }
        function step_two($message=''){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            
            if($message=='error'){
                $message='Anda harus mengisikan nilai instruktur';
            }
            $display['message']=$message;
            
            $angkatan=$this->session->userdata('angkatan');
            $kode=$this->session->userdata('kode');
            $cur_sesi=$this->session->userdata('no_sesi');
            
            $str_query = 'select * from user natural join ajar where KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan.' and NOMOR_SESI='.$cur_sesi.' and PERAN="instruktur"';
            $query_ins = $this->db->query($str_query);
            $display['data_ins'] = $query_ins->row_array();
            
            if($display['data_ins']['FOTO']==''){
                $display['data_ins']['FOTO']=base_url().'foto/cencored.jpg';
            }
            $str_query = 'select * from user natural join ajar where KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan.' and NOMOR_SESI='.$cur_sesi.' and PERAN="asisten"';
            $query_ass = $this->db->query($str_query);
            $query_ass = $query_ass->result_array();
            
            $display['jum_ass']=count($query_ass);
            
            if($display['jum_ass']==2){
                $display['ass1']=$query_ass[0];
                if($display['ass1']['FOTO']==''){
                    $display['ass1']['FOTO']=base_url().'foto/cencored.jpg';
                }
                $display['ass2']=$query_ass[1];
                if($display['ass2']['FOTO']==''){
                    $display['ass2']['FOTO']=base_url().'foto/cencored.jpg';
                }
            }else if($display['jum_ass']==1){
                $display['ass1']=$query_ass[0];
                if($display['ass1']['FOTO']==''){
                    $display['ass1']['FOTO']=base_url().'foto/cencored.jpg';
                }
            } 
            
            $count['num']=2;
            
            $this->load->view('feedback/feedback_header_view');
            $this->load->view('feedback/counter',$count);
            $this->load->view('feedback/step_two',$display);
            $this->load->view('feedback/feedback_footer_view');
        }
        function step_two_process(){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            
            if(!isset($_POST['rating_ins'])){
                redirect(redirect(base_url().'feedback/feedback_con/step_two/error'));
            }
            
            $id_penilai = $this->session->userdata('id');
            $angkatan=$this->session->userdata('angkatan');
            $kode=$this->session->userdata('kode');
            $cur_sesi=$this->session->userdata('no_sesi');
            $jum_ass = $this->input->post('jum_ass');
            
            $nilai_ins = array(
                'ID_PENILAI'=>$id_penilai,
                'ID_DINILAI'=>$this->input->post('inst_id'),
                'NILAI'=>$this->input->post('rating_ins'),
                'ABSTAIN'=>false,
                'KODE_PELATIHAN'=>$kode,
                'ANGKATAN'=>$angkatan,
                'NOMOR_SESI'=>$cur_sesi
            );
            
            $this->db->insert('nilai_pengajar',$nilai_ins);
            
            
            if($jum_ass>0){
                if(isset($_POST['abstain_ass1'])){
                    $stat_abstain=true;
                    $nilai=0;
                }else{
                    if(isset($_POST['rating_ass1'])){
                        $nilai=$this->input->post('rating_ass1');
                        $stat_abstain=false;
                    }else{
                        $stat_abstain=true;
                        $nilai=0;
                    }
                }
                $nilai_ass1 = array(
                    'ID_PENILAI'=>$id_penilai,
                    'ID_DINILAI'=>$this->input->post('ass1_id'),
                    'NILAI'=>$nilai,
                    'ABSTAIN'=>$stat_abstain,
                    'KODE_PELATIHAN'=>$kode,
                    'ANGKATAN'=>$angkatan,
                    'NOMOR_SESI'=>$cur_sesi
                );
                $this->db->insert('nilai_pengajar',$nilai_ass1);
            }

            if($jum_ass>1){
                if(isset($_POST['abstain_ass2'])){
                    $stat_abstain=true;
                    $nilai=0;
                }else{
                    $stat_abstain=false;
                    $nilai=$this->input->post('rating_ass2');
                }
                $nilai_ass2 = array(
                    'ID_PENILAI'=>$id_penilai,
                    'ID_DINILAI'=>$this->input->post('ass2_id'),
                    'NILAI'=>$nilai,
                    'ABSTAIN'=>$stat_abstain,
                    'KODE_PELATIHAN'=>$kode,
                    'ANGKATAN'=>$angkatan,
                    'NOMOR_SESI'=>$cur_sesi
                );
                $this->db->insert('nilai_pengajar',$nilai_ass2);
            }
            redirect(base_url().'feedback/feedback_con/step_three');
        }
        function step_three($message=''){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            
            $count['num']=3;
            
            $this->load->view('feedback/feedback_header_view');
            $this->load->view('feedback/counter',$count);
            $this->load->view('feedback/step_three');
            $this->load->view('feedback/feedback_footer_view');
        }
        function step_three_process(){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            $id = $this->session->userdata('id');
            $angkatan=$this->session->userdata('angkatan');
            $kode=$this->session->userdata('kode');
            $cur_sesi=$this->session->userdata('no_sesi');
            $komentar=$this->input->post('komentar');
            
            $this->db->where('ID',$id);
            $this->db->where('ANGKATAN',$angkatan);
            $this->db->where('KODE_PELATIHAN',$kode);
            $this->db->where('NOMOR_SESI',$cur_sesi);
            $data = array('KOMENTAR'=>$komentar);
            $this->db->update('komentar_sesi',$data);
            
            $data_sesi=array(
                'KODE_PELATIHAN'=>$kode,
                'ANGKATAN'=>$angkatan,
                'NOMOR_SESI'=>$cur_sesi
            );
            if($this->session->userdata('id_tema')!=-1){
                $set_pertanyaan_ke=array('pertanyaanke'=>1);
                $this->session->set_userdata($set_pertanyaan_ke);
                $query_id_pertanyaan = $this->db->get_where('daftar_pertanyaan',array('ID_TEMA'=>$this->session->userdata('id_tema')));
                $list_id_pertanyaan = $query_id_pertanyaan->result_array();
                for($i=0;$i<count($list_id_pertanyaan);$i++){
                    $set_pertanyaan[$i]=$list_id_pertanyaan[$i]['ID_PERTANYAAN'];
                }
                $this->session->set_userdata('list_pertanyaan',$set_pertanyaan);
                $this->session->set_userdata('offset_pertanyaan',1);
                redirect(base_url().'feedback/feedback_con/step_polls');
            }else{
                redirect(base_url().'feedback/feedback_con/end');
            }    
        }
        
        function step_polls(){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            $set_pertanyaan = $this->session->userdata('list_pertanyaan');
            $offset=$this->session->userdata('offset_pertanyaan');
            
            if($offset-1>=$this->session->userdata('jum_pertanyaan')){
                redirect(base_url().'feedback/feedback_con/end');
            }
            
            $display['data_pertanyaan']=$this->db->get_where('pertanyaan',array('ID_PERTANYAAN'=>$set_pertanyaan[$offset-1]))->row_array();
            $display['data_pilihan']=$this->db->get_where('daftar_pilihan',array('ID_PERTANYAAN'=>$set_pertanyaan[$offset-1]))->result_array();
            
            $count['num']=3+$offset;
            //$display['num']=3+$offset;
            $this->load->view('feedback/feedback_header_view');
            $this->load->view('feedback/counter',$count);
            $this->load->view('feedback/step_polls',$display);
            $this->load->view('feedback/feedback_footer_view');
            
        }
        
        function step_polls_process(){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            //di sini ngecek jenis pertanyaannya
            //jgn lupa ningkatin nilai offsetnya
            $id_pertanyaan = $this->input->post('id_pertanyaan');
            $tipe_pertanyaan = $this->input->post('tipe_pertanyaan');
            
            if($tipe_pertanyaan=='multi'){
                $jum_pil = $this->input->post('jum_pil');
                
                for($i=0;$i<$jum_pil;$i++){
                    if(isset($_POST['pilihan_'.$i])){
                        $data_poll = array(
                            'ID_PERTANYAAN'=>$id_pertanyaan,
                            'ID_USER'=>$this->session->userdata('id'),
                            'ID_PILIHAN'=>$this->input->post('pilihan_'.$i),
                            'KODE_PELATIHAN'=>$this->session->userdata('kode'),
                            'ANGKATAN'=>$this->session->userdata('angkatan'),
                            'NOMOR_SESI'=>$this->session->userdata('no_sesi')
                        );
                        $this->db->insert('jawaban',$data_poll);
                    }
                }
            }else if($tipe_pertanyaan=='single'){
                $pilihan = $this->input->post('pilihan');
                
                $data_poll = array(
                    'ID_PERTANYAAN'=>$id_pertanyaan,
                    'ID_USER'=>$this->session->userdata('id'),
                    'ID_PILIHAN'=>$pilihan,
                    'KODE_PELATIHAN'=>$this->session->userdata('kode'),
                    'ANGKATAN'=>$this->session->userdata('angkatan'),
                    'NOMOR_SESI'=>$this->session->userdata('no_sesi')
                );
                $this->db->insert('jawaban',$data_poll);
            }else if($tipe_pertanyaan=='essay'){
                $pilihan = $this->input->post('pilihan');
                
                $data_poll = array(
                    'ID_PERTANYAAN'=>$id_pertanyaan,
                    'ID_USER'=>$this->session->userdata('id'),
                    'JAWABAN'=>$pilihan,
                    'KODE_PELATIHAN'=>$this->session->userdata('kode'),
                    'ANGKATAN'=>$this->session->userdata('angkatan'),
                    'NOMOR_SESI'=>$this->session->userdata('no_sesi')
                );
                $this->db->insert('jawaban',$data_poll);
            }
            $cur_offset=$this->session->userdata('offset_pertanyaan');
            $this->session->set_userdata(array('offset_pertanyaan'=>($cur_offset+1)));
            redirect(base_url().'feedback/feedback_con/step_polls');
        }
        
        
        function end(){
            //mencegah yang masuk ke URL tanpa login
            if(!$this->session->userdata('login')){
                redirect(base_url().'feedback/feedback_con/error_display');
            }
            $id = $this->session->userdata('id');
            $angkatan=$this->session->userdata('angkatan');
            $kode=$this->session->userdata('kode');
            $cur_sesi=$this->session->userdata('no_sesi');
            $status = 'APPROVED';
            
            $data_absen = array(
                'ID'=>$id,
                'ANGKATAN'=>$angkatan,
                'KODE_PELATIHAN'=>$kode,
                'NOMOR_SESI'=>$cur_sesi,
                'STATUS'=>$status
            );
            $this->db->insert('absen',$data_absen);
            
            $this->session->sess_destroy();
            
            $this->load->view('feedback/feedback_header_view');
            $this->load->view('feedback/end_sf');
            $this->load->view('feedback/feedback_footer_view');
        }
        
        function error_display($jenis=0){
            $display['message_utama']='';
            $display['message_penjelasan']='';
            switch($jenis){
                case 0:
                    $display['message_utama']='Anda harus login terlebih dahulu';
                    break;
                case 1:
                    $display['message_utama']='Ada kesalahan pada username atau password anda';
                    break;
                case 2:
                    $display['message_utama']='Sekarang tidak ada SF yang tersedia '.$jam_sekarang = date('H.i',time());;
                    $display['message_penjelasan']='Silakan isi SF pada sesi yang aktif';
                    break;
                case 3:
                    $display['message_utama']='Anda sudah mengisi SF untuk sesi ini';
                    $display['message_penjelasan']='Silakan isi SF pada sesi yang lain';
                    break;
            }
            
            $this->load->view('feedback/feedback_header_view');
            $this->load->view('feedback/error_display',$display);
            $this->load->view('feedback/feedback_footer_view');
        }
        
    } 
?>
